<?php
/**
 * Created by PhpStorm.
 * User: GZ-1
 * Date: 2017/6/3
 * Time: 11:57
 */

header('Content-Type:text/html;charset=utf-8');

// 外网连接，在服务器外，便于测试
//$conn=mysqli_connect('592a9e6b3a4bd.gz.cdb.myqcloud.com:12521','root','lin385664','wxRecord','3306');

// 内网连接，在服务器上
$conn=mysqli_connect('10.66.239.203','root','lin385664','wxRecord','3306');

if(!$conn){
	echo json_encode(array(
		'stats'=>0,
		'msg'=>'无法连接数据库'
	));
	return;
}

// 设置中文
$sql = "SET NAMES UTF8";
mysqli_query($conn,$sql);


// 验证会话身份信息
function verifiedIdentityInfo($usid,$ssid){
    global $conn;
    $sql="SELECT `ssid` FROM `sessionKey` WHERE `usid`='$usid'";
    $result= mysqli_query($conn,$sql);
    $arr= mysqli_fetch_assoc($result);
    if($arr['ssid']==$ssid){
        return true;
    }else{
        return false;
    }
}

// 通过关键key/value对查找记录是否存在
function selectRecordByKey($table,$key,$val){
    global $conn;
    $sql="SELECT * FROM `$table` WHERE `$key`='$val'";
    $result= mysqli_query($conn,$sql);
    return mysqli_fetch_assoc($result);
}

// 保存sessionKey
function add_session_key_info($usid,$skey,$ssid,$loginWay,$timestamp){
    global $conn;
    $sql = "INSERT INTO `sessionKey` VALUES('$usid','$ssid', '$skey',$timestamp,$loginWay)";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        return true;
    } else {
        return false;
    }
}
function update_session_key_info($usid,$skey,$ssid,$loginWay,$timestamp){
    global $conn;
    $sql = "UPDATE `sessionKey` SET `ssid`='$ssid',`skey`='$skey',`updateTime`=$timestamp,`loginWay`=$loginWay WHERE `usid`='$usid'";
    $result = mysqli_query($conn, $sql);
    if($result){
        // 更新成功
        return true;
    }else{
        // 更新失败
        return false;
    }
}
function saveSessionKey($usid,$skey,$ssid,$loginWay){
    $sel_result=selectRecordByKey('sessionKey','usid',$usid);
    $timestamp = time();
    if($sel_result){
        // 已存在该行数据，更新ssid
        $result=update_session_key_info($usid,$skey,$ssid,$loginWay,$timestamp);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }else{
        // 不存在该行数据，插入新记录
        $result=add_session_key_info($usid,$skey,$ssid,$loginWay,$timestamp);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}

// 保存账号信息
function add_user_info($usid,$uflag, $uname, $upass, $nickName, $uphone, $orgName, $jobTitle, $setting, $utype){
    global $conn;
    $timestamp=time();
    $sql ="INSERT INTO `users` VALUES ('$usid', '$uflag','$uname', '$upass','$nickName', '$uphone', '$orgName', '$jobTitle', '$setting', $timestamp, $utype)";
    $result = mysqli_query($conn, $sql);
    if($result){
        return true;
    }else{
        return false;
    }
}
function update_user_info($usid,$uflag, $uname, $upass, $nickName, $uphone, $orgName, $jobTitle, $setting, $utype){
    global $conn;
    $timestamp=time();
    $sql ="UPDATE `users` SET `flag`='$uflag',`uname`='$uname',`upass`= '$upass',`nickName`='$nickName', `uphone`='$uphone', `orgName`='$orgName',`jobTitle`= '$jobTitle', `setting`='$setting',`updateTime`= $timestamp, `utype`=$utype WHERE `usid`='$usid'";
    $result = mysqli_query($conn, $sql);
    if($result){
        return true;
    }else{
        return false;
    }
}
function saveUserInfo($usid,$uflag, $uname, $upass, $nickName, $uphone, $orgName, $jobTitle, $setting, $utype){
    $sel_result=selectRecordByKey('users','usid',$usid);
    if($sel_result){
        // 已存在该行数据，更新ssid
        $result=update_user_info($usid,$uflag, $uname, $upass, $nickName, $uphone, $orgName, $jobTitle, $setting, $utype);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }else{
        // 不存在该行数据，插入新记录
        $result=add_user_info($usid,$uflag, $uname, $upass, $nickName, $uphone, $orgName, $jobTitle, $setting, $utype);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}

// 获取用户表数据，形成通讯录
function getAllUsers($usid){
    global $conn;
    $sql="SELECT * FROM `users`";
    $result= mysqli_query($conn,$sql);

    $output=array();
    while($row = mysqli_fetch_array($result)){
        $len=count($row);
        // 过滤其他数据
        for ($x=0;$x<$len;$x++){
            unset($row[$x]);
        }
        // 排除自己的数据
        if($row['usid']==$usid){
            continue;
        }
        $output[]=$row;
    }
    return $output;
}
